"use strict";

const { Controller } = require("egg");

class FrontController extends Controller {
  // 登录
  async login() {
    const { ctx } = this;
    const { username, password } = ctx.request.body;
    const post = await this.app.mysql.get("fontuser", {
      pass: password,
      user: username,
      type: "用户",
    });
    if (post) {
      ctx.body = {
        code: 200,
      };
    } else {
      ctx.body = {
        code: 300,
      };
    }
  }

  // 注册
  async reg() {
    const { ctx } = this;
    const { username, password, type } = ctx.request.body;
    const post = await this.app.mysql.get("fontuser", { pass: password });
    if (post) {
      ctx.body = {
        code: 300,
        msg: "用户已存在",
      };
    } else {
      const res = await this.app.mysql.insert("fontuser", {
        pass: password,
        user: username,
        type,
      });
      console.log(res)
      if (res) {
        ctx.body = {
          code: 200,
          msg: "注册成功，请登录",
        };
      } else {
        ctx.body = {
          code: 300,
          msg: "注册失败",
        };
      }
    }
  }

  // 房源详情
  async houseDetail(){
    const {ctx}=this;
    const {id}=ctx.query;
    const list=await this.app.mysql.get('houses',{key:id});
    ctx.body={
      code:200,
      list
    }
  }

  // 首页展示数据
  async getFirst() {
    const { ctx } = this;
    const {type,value}=ctx.query;
    const infoList = await this.app.mysql.select("info");
    const list = await this.app.mysql.select("houses");
    const brokerList = await this.app.mysql.select("broker");
    ctx.body = {
      code: 200,
      // 首页搜索房源
      list:list.filter((v)=>{
        if(!value) return true;
        return v.houses.includes(value)||v.address.includes(value)

      }).filter((v)=>{
        if(type==='全部'){
          return true;
        }
        if(type==='租房'){
          return v.type==='房屋租赁';
        }
        if(type==='买房'){
          return v.type==='新房售卖'||v.type==='二手房售卖';
        }
      }),
      infoList,
      hotList: list.filter(
        (v) =>
          v.hot === "热门" && v.name !== "未指派" && v.type1 !== "房源已提交"
      ),
      brokerList,
      twoList: list.filter((v) => v.hot !== "热门"),
    };
  }
  // 买房/租房数据
  async getBuyRoom() {
    const { ctx } = this;
    const { tabTitle, value,region,city } = ctx.query;
    const buyList = await this.app.mysql.select("houses", {
      where: { type: ["新房售卖", "二手房售卖"] },
    });
    const rentList = await this.app.mysql.select("houses", {
      where: { type: ["房屋租赁"] },
    });
    ctx.body = {
      code: 200,
      list: (tabTitle == "买房" ? buyList : rentList)
      .filter((v)=>{
        if(!city) return true;
        return v.address.includes(city)
      })
      .filter((v)=>{
        if(!region) return true;
        return v.address.includes(region)
      })
      .filter((v) => {
        if (!value || value == "") {
          return true;
        }
        return v.houses.includes(value)
      }),
    };
  }

  // 求租求购数据
  async getRentRoom() {
    const { ctx } = this;
    const { title, value } = ctx.query;
    console.log(title);
    const buyList = await this.app.mysql.select("font");
    ctx.body = {
      code: 200,
      list: buyList.filter((v) => v.type === title),
    };
  }

  // 资讯数据
  async getInfo() {
    const { ctx } = this;
    const list = await this.app.mysql.select("info");
    ctx.body = {
      code: 200,
      list,
    };
  }

  // 经纪人数据
  async getBroker() {
    const { ctx } = this;
    const {value}=ctx.query;
    const list = await this.app.mysql.select("broker");
    ctx.body = {
      code: 200,
      list:list.filter((v)=>{
        if(!value) return true;
        return v.name.includes(value)
      }),
    };
  }
  // 团购数据
  async getGroup() {
    const { ctx } = this;
    const list = await this.app.mysql.select("group");
    ctx.body = {
      code: 200,
      list,
    };
  }

  //直播数据
  async getLive() {
    const { ctx } = this;
    const { tab } = ctx.query;
    const list = await this.app.mysql.select("live");
    ctx.body = {
      code: 200,
      list: list.filter((v) => {
        if (tab === "不限") {
          return true;
        }
        if (tab === "正在直播") {
          return v.state == "直播中";
        }
        if (tab === "精彩回放") {
          return v.state == "已结束";
        }
      }),
    };
  }

  // 添加求租求购
  async addFont() {
    const { ctx } = this;
    const { name, tel, address, pic, type } = ctx.request.body;
    const list = await this.app.mysql.insert("font", {
      name,
      tel,
      address,
      pic,
      type,
    });
    if (list) {
      ctx.body = {
        code: 200,
      };
    } else {
      ctx.body = {
        code: 300,
      };
    }
  }

  // 添加房源
  async addHouses() {
    const { ctx } = this;
    const { houses, title, type, size, pic, address, url } = ctx.request.body;
    const list = await this.app.mysql.insert("audit", {
      houses,
      title,
      size,
      pic,
      city: address,
      type1: type,
      img: url,
    });
    if (list) {
      ctx.body = {
        code: 200,
      };
    } else {
      ctx.body = {
        code: 300,
      };
    }
  }
}

module.exports = FrontController;
